#include "dicomeditor.h"
#include "ui_dicomeditor.h"

#include <QJsonObject>

DicomEditor::DicomEditor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DicomEditor)
{
    ui->setupUi(this);
    init();
}

DicomEditor::~DicomEditor()
{
    delete ui;
}

void DicomEditor::init()
{
    connect(ui->rFlagBtn, SIGNAL(clicked()), ui->editView, SLOT(addRFlag()));
    connect(ui->lFlagBtn, SIGNAL(clicked()), ui->editView, SLOT(addLFlag()));
    connect(ui->fontBtn, SIGNAL(clicked()), ui->editView, SLOT(onFlagFont()));
    connect(ui->undoBtn, SIGNAL(clicked()), ui->editView, SLOT(undoImage()));
    connect(ui->redoBtn, SIGNAL(clicked()), ui->editView, SLOT(redoImage()));
    connect(ui->cutBtn, SIGNAL(clicked(bool)), ui->editView, SLOT(clipImage(bool)));
    connect(ui->negBtn, SIGNAL(clicked()), ui->editView, SLOT(inverseImage()));
    connect(ui->rotLeftBtn, SIGNAL(clicked()), ui->editView, SLOT(rotateCCW()));
    connect(ui->rotRightBtn, SIGNAL(clicked()), ui->editView, SLOT(rotateCW()));
    connect(ui->zoomBtn, SIGNAL(clicked()), ui->editView, SLOT(zoomImage()));
    connect(ui->hFlipBtn, SIGNAL(clicked()), ui->editView, SLOT(hFlip()));
    connect(ui->vFlipBtn, SIGNAL(clicked()), ui->editView, SLOT(vFlip()));
    connect(ui->panBtn, SIGNAL(clicked()), ui->editView, SLOT(panImage()));
    connect(ui->lineBtn, SIGNAL(clicked()), ui->editView, SLOT(drawLenght()));
    connect(ui->angleBtn, SIGNAL(clicked()), ui->editView, SLOT(drawAngle()));
    //connect(ui->polyBtn, SIGNAL(clicked()), ui->editView, SLOT());
    connect(ui->ellipseBtn, SIGNAL(clicked()), ui->editView, SLOT(drawEllipse()));
    connect(ui->rectBtn, SIGNAL(clicked()), ui->editView, SLOT(drawRectangle()));
    connect(ui->textBtn, SIGNAL(clicked()), ui->editView, SLOT(drawText()));
    connect(ui->arrowBtn, SIGNAL(clicked()), ui->editView, SLOT(drawArrow()));
    connect(ui->delBtn, SIGNAL(clicked()), ui->editView, SLOT(removeCurrent()));
    connect(ui->resetBtn, SIGNAL(clicked()), ui->editView, SLOT(reset()));
    connect(ui->magBtn, SIGNAL(clicked()), ui->editView, SLOT(magnifier()));

    connect(ui->editView, SIGNAL(imageClipped(bool)), ui->cutBtn, SLOT(setChecked(bool)));
}

void DicomEditor::setFileFormat(DcmFileFormat *dff)
{
    ui->editView->setFileFormat(dff);
}

void DicomEditor::on_okBtn_clicked()
{
    ui->editView->saveImage();
    if (m_proc) {
        QJsonObject json;
        json["action"] = "EditImageOk";
        m_proc(json, m_userData);
    }

    hide();
}

void DicomEditor::on_cancelBtn_clicked()
{
    if (m_proc) {
        QJsonObject json;
        json["action"] = "EditImageCancel";
        m_proc(json, m_userData);
    }

    hide();
}
